package com.web.onirigi.controller;


import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.web.onirigi.config.BaseConfig;
import com.web.onirigi.service.ImageService;
import com.web.onirigi.util.BaseResponseObject;
import com.web.onirigi.util.BaseUtil;
import com.web.onirigi.util.IdeaUtil;

/**
 * 图片上传服务
 * @author fengruiqi
 *
 */
@RestController
public class ImageRest {

	/**
	 * 日志组件
	 */
	private  static final Logger logger = LoggerFactory.getLogger(ImageRest.class);
	
	@Autowired
	private ImageService  imageServiceImpl;
	
	/**
	 * 图片保存路径
	 */
	@Value("${image.dir}")
	public  String IMAGE_DIR;
	
	/**
	 * 图片上传
	 * @param files
	 * @return
	 */
	@RequestMapping(value = "upload",method=RequestMethod.POST)
	public BaseResponseObject upload(@RequestParam("file")  MultipartFile  file
			,HttpServletResponse  respones){
		logger.info("单个图片上传...."+file.getSize());
		BaseResponseObject  object =new BaseResponseObject();
		try {
			String name=imageServiceImpl.saveImage(file);
			if(name != null){
				object.setResponseCode(BaseUtil.SUCCESS_CODE);
				object.setResponseMessage(name);
				return  object;
			}
			
		} catch (Exception e) {
			logger.error("上传图片失败。。",e);	
		}
		logger.info("单个图片上传成功...."+file.getSize());
		object.setResponseCode(BaseUtil.FAILED_CODE);
		object.setResponseMessage(BaseUtil.IMAGE_FAILED_CONTENT);
		return object;
	}
	
	/**
	 * 图片读取
	 * @author ASUS
	 * 创建时间  2017年10月8日 下午10:09:33
	 * @param imageName
	 * @param response
	 */
	 @RequestMapping(path = {"/image"}, method = {RequestMethod.GET})
	 @ResponseBody
     public void getImage(@RequestParam("name") String imageName,
                         HttpServletResponse response) {
        try {
            response.setContentType("image/jpeg");
            StreamUtils.copy(new FileInputStream(new
                    File(IMAGE_DIR + imageName)), response.getOutputStream());
        } catch (Exception e) {
            logger.error("读取图片错误" + imageName , e.getMessage());
        }
     }
	 
	 /**
	  * 上传多个图片
	  * @param file
	  * @param respones
	  * @return
	  */
 	 @RequestMapping(value = "uploads",method=RequestMethod.POST)
	 public BaseResponseObject uploads(@RequestParam("files")  MultipartFile[]  file
			,HttpServletResponse  respones){
 		logger.info("开始上传图片。。。图片内容{}"+file);
		BaseResponseObject  object =new BaseResponseObject();
		try {
			List<String> list=imageServiceImpl.saveImages(file);
			if(list != null || list.size()>0){
				Map<String,List> map =new HashMap<>(); 
				map.put("images", list);
				object.setResponseCode(BaseUtil.SUCCESS_CODE);
				object.setResponseData(map);
				object.setResponseMessage(BaseUtil.HOSTL_SUCCESS_SUBMIT);
				return  object;
			}
		} catch (Exception e) {
			logger.info("上传图片失败。。。",e);
			logger.error("上传图片失败。。",e);	
		}
		
		object.setResponseCode(BaseUtil.FAILED_CODE);
		object.setResponseMessage(BaseUtil.IMAGE_FAILED_CONTENT);
		logger.info("上传图片成功。。。图片内容{}"+file);
		return object;
	 }
 	 
 	 /**
	  * 上传图片利用Base64编码
	  * @param file
	  * @param respones
	  * @return
	  */
 	 @RequestMapping(value = "uploadBase",method=RequestMethod.POST)
	 public BaseResponseObject uploads(@RequestBody  String file
			,HttpServletResponse  respones){
 		respones.setHeader("Access-Control-Allow-Origin", "*"); 
		BaseResponseObject  object =new BaseResponseObject();
		logger.info("开始上传图片。。。图片内容{}"+file);
		try {
			String imageName=imageServiceImpl.saveImageFromBase(file);
			if(imageName !=null && !"".equals(imageName) ) {
				Map<String,String> map =new HashMap<>(); 
				map.put("images", imageName);
				object.setResponseCode(BaseUtil.SUCCESS_CODE);
				object.setResponseData(map);
				object.setResponseMessage(BaseUtil.HOSTL_SUCCESS_SUBMIT);
				logger.info("是否失败");
				return  object;
			}
		
		} catch (Exception e) {
			logger.info("上传图片失败。。。",e);
		}
		
		object.setResponseCode(BaseUtil.FAILED_CODE);
		object.setResponseMessage(BaseUtil.IMAGE_FAILED_CONTENT);
		return object;
	 }

 	 /**
 	  * 删除指定图片
 	  * @author ASUS
 	  * 创建时间  2017年10月14日 下午6:12:30
 	  * @return
 	  */
 	 @RequestMapping(value = "delete",method =RequestMethod.POST)
 	 public BaseResponseObject  deleteImage() {
 		 BaseResponseObject object =new BaseResponseObject() ;
 		 
 		 
 		 return object;
 	 }
 	 
 	 
}
